package com.yy.device.api.web;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Arrays;

/**
 * web通用异常定义
 *
 * @author sunzhiqiang
 * @since 4.0.0
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class APIException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 0 error
     * 1 warn
     * 2 info
     */
    @Default
    private int level = 0;

    /**
     * 错误码
     */
    private int code;
    /**
     * 错误描述
     */
    private String msg;
    /**
     * 错误描述中占位符所对应的参数
     */
    private transient Object[] args;


    public APIException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public APIException(int code, String msg, Object... args) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }


    @Override
    public String toString() {
        return "APIException{"
                + "code=" + code
                + ", msg='" + msg + '\''
                + ", args=" + Arrays.toString(args) + '}';
    }
}
